<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.keyword" placeholder="" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item label="所属乡镇：">
          <el-select v-model="listPram.township" >
            <el-option label="全部" :value="0"></el-option>
            <el-option v-for="item in township" :key="item.id" :label="item.townshipName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button :disabled="!conf" icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0)" v-has-permi="['admin:food:save']">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list"  v-loading="loading" size="mini" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column
          prop="id"
          label="ID"
          width="50"
        />
        <el-table-column label="标题" prop="title" width="120"/>
        <el-table-column label="地址" prop="address" min-width="120"/>
        <el-table-column label="描述" prop="account" min-width="120"/>
        <el-table-column label="电话" prop="phone" min-width="120"/>
        <el-table-column label="乡镇" prop="townshipText" width="120"/>
        <el-table-column label="排序" prop="sort" width="120"/>
        <el-table-column label="显示" width="140">
          <template slot-scope="scope">
            <el-switch
              :disabled="true"
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              active-text="显示"
              inactive-text="隐藏"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="130" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button :disabled="!conf" icon="el-icon-edit" type="primary" size="mini" @click="handlerOpenEdit(1,scope.row)" v-has-permi="['admin:food:save']">编辑</el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handlerOpenDel(scope.row.id)" v-has-permi="['admin:food:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.count"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
      :visible.sync="editDialogConfig.visible"
      :title="editDialogConfig.isCreate === 0? '新增':'编辑'"
      destroy-on-close
      top="80px"
      :close-on-click-modal="false"
      width="1100px"
    >
      <edit
        v-if="editDialogConfig.visible"
        :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.editData"
        :township="township"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  deleteRow
} from '@/api/food'
import edit from './edit'
import {list as townshipList} from '@/api/township';
export default {
  components: { edit },
  data() {
    return {
      constants:this.$constants,
      loading:false,
      listData: { list: [] },
      listPram: {
        page: 1,
        limit: this.$constants.page.limit[0],
        keyword:'',
        township:0
      },
      editDialogConfig: {
        visible: false,
        isCreate: 0,
        editData: {}
      },
      township:[],
      conf:false,
    }
  },
  mounted() {
    this.handleGetList()
    this.initConf();
  },
  methods: {
    initConf(){
      this.conf=false;
      townshipList({page:1,limit:100}).then(res=>{
        this.township=res;
      }).finally(()=>{
        this.conf=true;
      })
    },
    handleSearch() {
      this.listPram.page = 1
      this.handleGetList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetList()
    },
    handlerOpenDel(id) {
      this.$confirm('确认删除当前数据').then(() => {
        deleteRow({id}).then(data => {
          this.$message.success('删除数据成功')
          this.handleGetList()
        })
      })
    },
    handleGetList() {
      this.loading=true
      list( this.listPram ).then(data => {
        this.listData.count = data.count
        this.listData.list = data.list
      }).finally(()=>{
        this.loading=false;
      })
    },
    handlerOpenEdit(isCreate, editDate) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.handleGetList()
    },
    resetQuery(){
      this.listPram.keyword = '';
      this.listPram.township = 0;
      this.handleGetList()
    }
  }
}
</script>
